package com.myapp.ruyixing.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.Message;
import com.myapp.ruyixing.ui.widget.PhotoPickFragment.PhotoPickCompleteListener;
import com.myapp.ruyixing.ui.widget.PhotoTypePickFragment;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.ImageUtils;
import com.myapp.ruyixing.utils.LocationUtils;

public class AddShopActivity extends BaseActivity implements
		PhotoPickCompleteListener, BDLocationListener, OnClickListener {
	LocationClient mLocationClient;
	BDLocation mLocation = null;

	private String mUploadFile;

	private View mLoadingView;
	private TextView mLonLat;

	private RYHttpResponseHandler mUploadHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			mLoadingView.setVisibility(View.GONE);

			Message message = (Message) o;
			if (message != null) {
				Toast.makeText(AddShopActivity.this, "添加成功", Toast.LENGTH_SHORT)
						.show();
			}

			if (message.errno == 0) {
				finish();
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			mLoadingView.setVisibility(View.GONE);
			Toast.makeText(AddShopActivity.this, "添加失败", Toast.LENGTH_SHORT)
					.show();
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_add_shop);

		findViewById(R.id.layout_map).setOnClickListener(this);

		mLocationClient = new LocationClient(this,
				LocationUtils.getDefaultOption());
		mLocationClient.registerLocationListener(this);
		mLocationClient.start();
		mLocationClient.requestLocation();

		mLoadingView = findViewById(R.id.my_loading);
		mLoadingView.setVisibility(View.VISIBLE);
		mLonLat = (TextView) findViewById(R.id.text_lonlat);
		findViewById(R.id.edit_channel).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						startActivityForResult(new Intent(AddShopActivity.this,
								ChooseCityMenuActivity.class), 100);
					}
				});

		findViewById(R.id.upload).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new PhotoTypePickFragment().show(getSupportFragmentManager()
						.beginTransaction(), "photo");
			}
		});

		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});

		((TitleBar) findViewById(R.id.title_bar))
				.setOnRightIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						commit();
					}
				});
	}

	@Override
	protected void onResume() {
		if (mLocation == null) {
			mLocationClient.start();
		}
		super.onResume();
	}

	@Override
	protected void onPause() {
		mLocationClient.stop();
		super.onPause();
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);

		if (arg0 == 100 && arg1 == RESULT_OK && arg2 != null) {
			String typeid = arg2
					.getStringExtra(NearbyStoreActivity.INTENT_KEY_TYPEID);
			String typeName = arg2
					.getStringExtra(NearbyStoreActivity.INTENT_KEY_TYPE_NAME);

			((TextView) findViewById(R.id.edit_channel)).setText(typeName);
		} else if (arg0 == 888 && arg1 == RESULT_OK && arg2 != null) {
			String address = arg2.getStringExtra("address");
			int lat = arg2.getIntExtra("lat", 0);
			int lon = arg2.getIntExtra("lon", 0);
			if (!TextUtils.isEmpty(address)) {
				((EditText) findViewById(R.id.edit_address)).setText(address);
			}
			if (lat != 0 && lon != 0) {
				mLocationClient.stop();
				mLocation = new BDLocation();
				mLocation.setLongitude(((double) lon) / 1E6);
				mLocation.setLatitude(((double) lat) / 1E6);
				mLonLat.setText(String.format("(%.1f, %.1f)",
						mLocation.getLongitude(), mLocation.getLatitude()));
			}
		}
	}

	@Override
	public void onPhotoPicker(String file) {
		mUploadFile = file;

		ImageView iv = (ImageView) findViewById(R.id.upload);
		iv.setOnClickListener(null);

		ImageUtils.displayImage(file, iv);
	}

	private void commit() {
		String name = ((EditText) findViewById(R.id.edit_name)).getText()
				.toString();

		if (TextUtils.isEmpty(name) == true) {
			Toast.makeText(this, "商户名不能为空", Toast.LENGTH_LONG).show();
			return;
		}

		String channel = ((TextView) findViewById(R.id.edit_channel)).getText()
				.toString();

		if (TextUtils.isEmpty(channel) == true || "请选择频道".equals(channel)) {
			Toast.makeText(this, "频道不能为空", Toast.LENGTH_LONG).show();
			return;
		}

		String address = ((EditText) findViewById(R.id.edit_address)).getText()
				.toString();

		if (TextUtils.isEmpty(address) == true) {
			Toast.makeText(this, "地址不能为空", Toast.LENGTH_LONG).show();
			return;
		}

		String nearby = ((EditText) findViewById(R.id.edit_nearby)).getText()
				.toString();
		String phone = ((EditText) findViewById(R.id.edit_phone)).getText()
				.toString();
		String day = ((EditText) findViewById(R.id.edit_day)).getText()
				.toString();
		String time = ((EditText) findViewById(R.id.edit_day_of_time))
				.getText().toString();
		String people = ((EditText) findViewById(R.id.edit_people)).getText()
				.toString();

		mLoadingView.setVisibility(View.VISIBLE);
		RYHttpClient.addShop(this, mUploadHandler, name, mUploadFile, channel,
				address, nearby, phone, day, time, people,
				(mLocation == null ? 0 : mLocation.getLatitude()) + "",
				(mLocation == null ? 0 : mLocation.getLongitude()) + "");
	}

	@Override
	public void onReceiveLocation(BDLocation arg0) {
		if (arg0 != null && arg0.getLatitude() > 0.1
				&& arg0.getLongitude() > 0.1) {
			mLocation = arg0;
			if (mLoadingView != null) {
				mLoadingView.setVisibility(View.GONE);
			}
			if (mLonLat != null) {
				mLonLat.setText(String.format("(%.1f, %.1f)",
						arg0.getLongitude(), arg0.getLatitude()));
			}
			mLocationClient.stop();
		}
	}

	@Override
	public void onReceivePoi(BDLocation arg0) {

	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(this, ChooseLocationActivity.class);
		startActivityForResult(intent, 888);
	}

}
